var app = getApp();
const quote = app.globalData.quote;
const api = quote.api;
Page({

  /**
   * 页面的初始数据
   */
  data: {
      isBrang: app.globalData.isBrang,
      score:0,
      isUnlock:true,//判断此关卡是否得过60分
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
      var that=this
      app.globalData.isBack=true,
      wx.showLoading({
          title: '加载中...',
      })
      that.setData({
          score: parseFloat(options.score)
      })
      that.getNextId(2)
      //超过80分将解锁次关卡
      var score = parseFloat(options.score)
      if(score>=60){
          that.getNextId(1)
      }
      console.log(options,that.data.score)

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },


  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
      return {
          path: 'pages/indexPackage/index/index',
          success: function (res) {
              console.log('转发成功', res);
          },
          fail: function (err) {
              console.log('转发失败', err)
          }
      }
  },
  //判断当前模块是否解锁
    ifUnlock: function (moduleId){
      var dataDic = {
          ModuleId: moduleId,
          OpenId: app.globalData.openId,
          SubtitleId:1
      };
      var that = this
      api.methodBasicEnglish_ModuleSubtitleIsUnlock({
          dataDic: dataDic,
          success:function(res){
              console.log('获取解锁状况成功',res)
              wx.hideLoading()
              that.setData({
                  isUnlock: res.data.data.IsUnlock
              })
            //   if (res.data.data.IsUnlock){
            //       that.setData({
            //           isUnlock:true
            //       })
            //   }else{
            //       that.setData({
            //           isUnlock: false
            //       })
            //   }
          },
          fail:function(err){
              console.log('获取解锁状况失败', err)
          },
      })
  },
  //获取下一块模块ID
  getNextId:function(type){
      var dataDic = {
          ModuleId: app.globalData.moduleId,
      };
      var that=this
      api.methodBasicEnglish_NextModuleId({
          dataDic: dataDic,
          success:function(res){
              console.log('获取下一个模块ID成功',res)
              var nextId = res.data.data.NextModuleId
              setTimeout(()=>{
                  if(type==1){
                      that.unlockLevels(nextId)
                  }else if(type == 2){
                      that.ifUnlock(nextId)
                  }
                  
              },1000)           
          },
          fail:function(err){
              console.log('获取下一个模块ID失败', err)
          }
      })
  },
   
//解锁
    unlockLevels: function (moduleId) {
        var dataDic = {
            ModuleId: moduleId,           
            SubtitleId: 1,
            OpenId: app.globalData.openId
        };
        var that = this
        api.methodBasicEnglish_Unlock({
            dataDic: dataDic,
            success: function (res) {
                console.log('解锁成功', res)
            },
            fail: function (err) {
                console.log('解锁失败', err)
            }
        })

    },
    finishPractice:function(){
        var that=this
        app.globalData.isBack = false
        if(app.globalData.addId == 0){
            app.globalData.addId = 1
            
           wx.reLaunch({
             url: '../../../../pages/indexPackage/courseList/courseList?addId='+ 1,
           })
         }else{
           console.log('走缓存处理')
           wx.getStorage({
             key: 'removes',
             success: (res) => {
               console.log(res.data)
               if(res.data){
                 console.log('426')
                 wx.reLaunch({
                   url: '../../../../pages/indexPackage/courseList/courseList?addId=' + 2,
                 })
               }
             },
         
           })
         }

            
    },
    goBack: function () {
        var that = this
        wx.reLaunch({
            url: '/pages/indexPackage/courseList/courseList',
        })
    },
    beginSayAgain: function () {
        wx.navigateBack({
            delta:1
        })
        // wx.redirectTo({
        //     url: '/pages/taskPackage/pages/taskFour/taskFour?selectIndex=2',
        // })
    },
})